package de.hbrs.ooka.beans;

import de.hbrs.ooka.entities.PaperEntity;
import de.hbrs.ooka.entities.UserEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class PaperBeanStateless implements PaperBeanStatelessLocal {

    @PersistenceContext(unitName = "CRS_PU")
    private EntityManager em;

    @Override
    public PaperEntity getPaper(int id) {
        Query q = em.createNamedQuery("findPaperByID").setParameter("id", id);
        List<PaperEntity> resultList = q.getResultList();

        if (resultList.size() != 1) {
            throw new IllegalArgumentException("Wrong Query: " + resultList.size());
        }
        return resultList.get(0);
    }

    @Override
    public PaperEntity getPaper(String name) {
        Query q = em.createNamedQuery("findPaperByName", PaperEntity.class).setParameter("name", name);
        List<PaperEntity> resultList = q.getResultList();

        if (resultList.size() != 1) {
            throw new IllegalArgumentException("Wrong Query: " + resultList.size());
        }
        return resultList.get(0);
    }

    @Override
    public boolean updatePaper(PaperEntity paper) {
        if (em.merge(paper) != null) {
            return true;
        }
        return false;
    }
    public static final String FILE_EXTENSION = ".pdf";

    @Override
    public PaperEntity checkFile(String filepath, UserEntity author) {
        if (filepath == null || filepath.length() < 1) {
            return null;
        }

        if (filepath.toLowerCase().trim().matches("[\\w]+" + FILE_EXTENSION)) {
            PaperEntity p = new PaperEntity(filepath, filepath.substring(0, filepath.indexOf(FILE_EXTENSION)), "LOL CONTENT SHIT");
            p.setAuthor(author);

            return p;
        }

        return null;
    }

    @Override
    public void reviewPaper(UserEntity reviewer, String paper, int grade, boolean passed, String reasons) {
        System.out.println("REVIEW PAPER !!! " + paper);

        PaperEntity p = this.getPaper(paper);

        p.setAccepted(passed);
        p.setRating(grade);
        p.setTextualRating(reasons);
        
        p.getReviewer().remove(reviewer);

        em.merge(p);
    }

    @Override
    public List<PaperEntity> getAllPapers() {
        Query qu = em.createNamedQuery("findAllPapers");
        return qu.getResultList();
    }

    @Override
    public PaperEntity uploadPaper(PaperEntity paper) {
        if (paper != null) {
            em.persist(paper);
            return paper;
        }

        throw new UnsupportedOperationException("Wrong Usage of this Method !");
    }

    @Override
    public void assignToReviewer(UserEntity user, String paperName) {
        PaperEntity paper = this.getPaper(Integer.parseInt(paperName));
        paper.addReviewer(user);
        em.merge(paper);
        System.out.println("PaperBeanStateless, assign: " + paper.getFilename());
    }
}
